/** 
 * 绘制按钮
 */
class DrawBtn extends egret.DisplayObjectContainer {
    /** 触摸开始的初始位置 */
    private distance: egret.Point = new egret.Point();
    /** 为双击事件记录点击时间 */
    private beforeTouch: number = new Date().getTime();
    /** 当前操作的按钮 */
    private nowControl: egret.Shape;
    /** 按钮数据 */
    private btnData = RES.getRes("ditieBtnPath_json");

    /** 按钮保护 */
    private saveBtn: number = 0;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {

        for (let data of this.btnData) {
            this.createBtn(data);
        }

    }
    /** 创建按钮 */
    private createBtn(data) {
        const btn = new egret.Sprite();
        this.addChild(btn);
        btn.x = data.x;
        btn.y = data.y;
        btn.name = data.title;
        const txt = new egret.TextField();
        txt.text = data.title;
        // btn.addChild(txt);
        btn.graphics.beginFill(0xf45050, 0);
        btn.graphics.drawRect(0, 0, 100, 100);
        btn.graphics.endFill();
        btn.touchEnabled = true;
        /** 拖动 */
        btn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchStartCallBack, this);
        /** 点击弹窗 */
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.alertEvent, this);
    }
    /** 拖动开始 */
    private touchStartCallBack(evt: egret.TouchEvent): void {

        Main.superStopPropagation(evt);
        this.nowControl = evt.target;
        /** 保存初始位置 */
        this.distance.x = evt.stageX;
        this.distance.y = evt.stageY;
        /** 判断单双击 */
        if (new Date().getTime() - this.beforeTouch > 300) {
            this.beforeTouch = new Date().getTime();
            return;
        }

        this.nowControl.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMoveCallBack, this);
        this.stage.once(egret.TouchEvent.TOUCH_END, this.touchEndCallBack, this);
    }
    /** 拖动 */
    private touchMoveCallBack(evt: egret.TouchEvent): void {
        Main.superStopPropagation(evt);
        /** 计算移动后位置 */
        const computedX = evt.stageX - this.distance.x;
        const computedY = evt.stageY - this.distance.y;
        this.saveBtn = Math.abs(computedX) + Math.abs(computedX);
        console.log(this.saveBtn)
        /** 查看是否开启编辑模式 */
        if (!BuildControl.isBuild) return;

        /** 初始化位置 */
        this.distance.x = evt.stageX;
        this.distance.y = evt.stageY;

        this.nowControl.x += computedX;
        this.nowControl.y += computedY;
    }
    /** 拖动结束 */
    private touchEndCallBack(evt: egret.TouchEvent): void {
        Main.superStopPropagation(evt);
        /** 消除按钮保护 */
        this.saveBtn = 0;
        /** 消除拖动事件 */
        this.nowControl.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMoveCallBack, this);
        /** 坐标收集 */
        const bufferBtn = this.btnData.find((obj) => obj.title == evt.target.name);
        bufferBtn.x = evt.target.x;
        bufferBtn.y = evt.target.y;
        localStorage.setItem("btnPath", JSON.stringify(this.btnData));
        console.log(this.btnData)
    }
    /** 弹窗  */
    private alertEvent(evt: egret.TouchEvent) {
        if (BuildControl.isBuild) return;
        if (this.saveBtn > 500) return;
        /** 获取对应数据 */
        const data = this.btnData.find(obj => obj.title == evt.target.name);
        /**弹窗容器 */
        const container = new egret.Sprite();
        this.stage.addChild(container);
        container.graphics.beginFill(0, .5);
        container.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        container.graphics.endFill();
        container.touchEnabled = true;
        container.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Main.superStopPropagation, this);
        /** 弹窗背景图 */
        let alertImg;
        if (data.change)
            alertImg = Main.createBitmapByName(`change_${data.change}_png`)
        else
            alertImg = Main.createBitmapByName(`alert_${data.alertId}_png`);
        const oH = alertImg.width / alertImg.height;
        container.addChild(alertImg);
        alertImg.width = this.stage.stageWidth * .9;
        alertImg.height = alertImg.width / oH;
        Main.anchorCenter(alertImg);
        alertImg.x = this.stage.stageWidth * .5;
        alertImg.y = this.stage.stageHeight * .5;
        /** 弹窗标题文字 */
        const title_bg = new egret.Sprite();
        container.addChild(title_bg);
        const alertTitle = new egret.TextField();
        container.addChild(alertTitle);
        if (data.change)
            alertTitle.text = ` 换乘站 - ${data.title}`
        else
            alertTitle.text = ` ${data.btnID}号线 - ${data.title}`;
       
        alertTitle.x = 1055 - 20;
         alertTitle.anchorOffsetX = alertTitle.width;
        alertTitle.y = alertImg.y + alertImg.height /2 -100;
        let color = 0xf45050;
        if (data.btnID == 1)
            color = 0x074957
        else if (data.btnID == 2)
            color = 0xf45050
        else if (data.btnID == 3)
            color = 0xFD6DCC
        else if (data.btnID == 4)
            color = 0x00CEBA;
        title_bg.graphics.beginFill(color, 1);
        title_bg.graphics.drawRect(0, 0, alertTitle.width, 80);
        title_bg.graphics.drawArc(alertTitle.width, 40, 40, 0, Math.PI * -90);
        title_bg.graphics.endFill();
        title_bg.x = 1055;
        title_bg.y = 480;
        title_bg.scaleX = -1;
        /** 弹窗关闭按钮 */
        const closeBtn = new egret.Sprite();
        container.addChild(closeBtn);
        closeBtn.graphics.beginFill(0, 0);
        closeBtn.graphics.drawRect(0, 0, 80, 80);
        closeBtn.graphics.endFill();
        closeBtn.anchorOffsetX = closeBtn.width;
        closeBtn.x = alertImg.x + alertImg.width / 2 - 30;
        closeBtn.y = 50;
        closeBtn.touchEnabled = true;
        closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.stage.removeChild(container);
        }, this);
    }

}